package net.kazed.nextaction.database;

import net.kazed.android.database.Table;
import android.content.ContentValues;
import android.database.Cursor;
import android.provider.BaseColumns;

/**
 * Definition of Context table.
 */
public class ContextTable extends Table<GtdContext> {
    
    public static final String TABLE = "context";
    public static final String COLUMN_ID = BaseColumns._ID;
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_COLOR = "colour";
    public static final String COLUMN_ICON_ID = "iconId";
    public static final String COLUMN_NUMBER_OF_TASKS = "numberOfTasks";
    public static final String COLUMN_PARENT_CONTEXT_ID = "parentContextId";

    /**
     * Constructor.
     */
    public ContextTable() {
        super(TABLE, COLUMN_NAME);
        addColumn(COLUMN_ID);
        addColumn(COLUMN_NAME);
        addColumn(COLUMN_COLOR);
        addColumn(COLUMN_ICON_ID);
        addColumn(COLUMN_NUMBER_OF_TASKS);
        addColumn(COLUMN_PARENT_CONTEXT_ID);
    }
    
    /**
     * Extract object from cursor.
     * @param cursor Query cursor.
     * @return Extracted object.
     */
    public GtdContext extract(final Cursor cursor) {
        final int id = getInteger(cursor, COLUMN_ID);
        final String name = getString(cursor, COLUMN_NAME);
        final int colorIndex = getInteger(cursor, COLUMN_COLOR);
        final int iconResourceId = getInteger(cursor, COLUMN_ICON_ID);
        final int numberOfTasks = getInteger(cursor, COLUMN_NUMBER_OF_TASKS);
        final int parentContextId = getInteger(cursor, COLUMN_PARENT_CONTEXT_ID);
        final GtdContext context = new GtdContext(id, name, colorIndex, iconResourceId);
        context.setNumberOfTasks(numberOfTasks);
        context.setParentContextId(parentContextId);
        return context;
    }

    /**
     * Insert fragment values into collection.
     * @param fragment Model object.
     * @return Content values.
     */
    public ContentValues getValues(final GtdContext context) {
        final ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, context.getName());
        values.put(COLUMN_COLOR, context.getColourIndex());
        values.put(COLUMN_ICON_ID, context.getIconResource());
        values.put(COLUMN_NUMBER_OF_TASKS, context.getNumberOfTasks());
        values.put(COLUMN_PARENT_CONTEXT_ID, context.getParentContextId());
        return values;
    }

}
